'use strict';
define(['routes','services/dependencyResolverFor'], function(config, dependencyResolverFor)
{
    'use strict';

    //初始注入
    var app = angular.module('app', ['ionic']);

    app.config(
    [
        '$locationProvider',
        '$controllerProvider',
        '$compileProvider',
        '$filterProvider',
        '$provide',
        '$stateProvider',
        '$animateProvider',

        function( $locationProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $stateProvider ,$animateProvider)
        {
            //注册控制器，服务
            app.controller = $controllerProvider.register;
            app.directive  = $compileProvider.directive;
            app.filter     = $filterProvider.register;
            app.animation  = $animateProvider.register;
            app.factory    = $provide.factory;
            app.service    = $provide.service;
            app.provider   = $provide.provider;

            //html5路由模式
            //$locationProvider.html5Mode(true);

            //生成路由
            if(config.routes !== undefined)
            {
                angular.forEach(config.routes, function(route, path)
                {
                    //多个view
                    if(route.views){
                        $stateProvider.state(path, {
                            url: route.url,
                            abstract: route.abstract,
                            views: route.views,
                            resolve: dependencyResolverFor(route.dependencies),
                            data: route.data,
                            onEnter: route.onEnter,
                            cache: route.cache,
                            //页面安全系数
                            ACCESS_LEVEL: route.ACCESS_LEVEL
                        });
                    }
                    //单个view
                    else{
                        $stateProvider.state(path, {
                            url: route.url,
                            templateUrl: route.templateUrl,
                            abstract: route.abstract,
                            controller: route.controller,
                            resolve: dependencyResolverFor(route.dependencies),
                            data: route.data,
                            onEnter: route.onEnter,
                            cache: route.cache,
                            //页面安全系数
                            ACCESS_LEVEL: route.ACCESS_LEVEL
                        });
                    }
                });
            }
        }
    ]);

   return app;
});